package com.ti.demo.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.vectorstore.VectorStoreChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @description: com.ti.demo.controller
 * @Author Triagen
 * @Date 2025/7/18 10:53
 */
@RestController
public class ChatController {

    @Qualifier("ollamaChatClient")
    @Autowired
    private ChatClient ollamaChatClient;

    @Qualifier("openaiChatClient")
    @Autowired
    private ChatClient openaiChatClient;

    @Qualifier("poetClient")
    @Autowired
    private ChatClient poetClient;

    @Qualifier("qaClient")
    @Autowired
    private ChatClient qaClient;

    @Qualifier("raClient")
    @Autowired
    private ChatClient raClient;


    @GetMapping("/ollama/chat")
    Flux<String> ollamaChat(String conversationId, String userInput) {
        return this.ollamaChatClient.prompt()
                // Set advisor parameters at runtime
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId))
                .user(userInput)
                .stream()
                .content();
    }

    @GetMapping("/openai/chat")
    Flux<String> openaiChat(String conversationId, String userInput) {
        return this.openaiChatClient.prompt()
                // Set advisor parameters at runtime
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId)) // 设置会话id
                .user(userInput)
                .stream()
                .content();
    }

    @GetMapping("/poet/chat")
    Flux<String> poetChat(String conversationId, String userInput) {
        return this.poetClient.prompt()
                // Set advisor parameters at runtime
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId)) // 设置会话id
                .advisors(advisorSpec -> advisorSpec.param(VectorStoreChatMemoryAdvisor.TOP_K, 15))  // 设置 top_k
                .user(userInput)
                .stream()
                .content();
    }

    @GetMapping("/qa/chat")
    Flux<String> qaChat(String conversationId, String userInput) {
        return this.qaClient.prompt()
                // Set advisor parameters at runtime
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId)) // 设置会话id
                .advisors(advisorSpec -> advisorSpec.param(VectorStoreChatMemoryAdvisor.TOP_K, 15))  // 设置 top_k
                .user(userInput)
                .stream()
                .content();
    }

    @GetMapping("/ra/chat")
    Flux<String> raChat(String conversationId, String userInput) {
        return this.qaClient.prompt()
                // Set advisor parameters at runtime
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, conversationId)) // 设置会话id
                .advisors(advisorSpec -> advisorSpec.param(VectorStoreChatMemoryAdvisor.TOP_K, 15))  // 设置 top_k
                .user(userInput)
                .stream()
                .content();
    }
}
